import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;

//ABSTRACT CLASS - Do not instantiate
class AVerletView extends Sprite
{
private var _shape:Shape;
  
  //Variable that refers to the model.
  //Note that it's "protected"
  //so that it can be accessed by other classes 
  //in this same package, but not outside it
  public var model:AVerletModel;

  public function new(verletModel:AVerletModel)
  {
    super();
    model = verletModel;
    model.addEventListener(Event.CHANGE, changeHandler);
    model.addEventListener("destory", deleteHandler);
    addEventListener(Event.ADDED_TO_STAGE, addedToStagHandler);
  }

  private function deleteHandler(event:Event)
  {
    model.removeEventListener(Event.CHANGE, changeHandler);
    model.removeEventListener("destory", deleteHandler);
    model = null;
  }
  private function addedToStagHandler(event:Event)
  {
    //Draw the object
    draw();
    
    //Position object
    this.x = model.xPos;
    this.y = model.yPos;
    
    //Add listeners
    addEventListener
      (Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  }
  
  private function removedFromStageHandler(event:Event)
  {
    removeEventListener
      (Event.ADDED_TO_STAGE, addedToStagHandler);
    removeEventListener
      (Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  }
  
  //The draw method is also protected
  private function draw()
  {

  }
  
  private function changeHandler(event:Event)
  {
    this.x = model.xPos;
    this.y = model.yPos;
    
    //If any of your objects use rotation, add this line:
    this.rotation = model.angle;
    
    //If any of your objects depend on visiblity, use this:
    this.visible = model.visible;
  }
}